(function (window) {
    function Progress($progressBar, $progressLoad, $progressPlay) {
        return new Progress.prototype.init($progressBar, $progressLoad, $progressPlay);
    }
    Progress.prototype = {
        constructor: Progress,
        isMove: false,
        init: function ($progressBar, $progressLoad, $progressPlay) {
            this.$progressBar = $progressBar;
            this.$progressLoad = $progressLoad;
            this.$progressPlay = $progressPlay;
        },
        progressClick: function (callBack) {
            var $this = this;
            // 监听滚动条点击事件
            this.$progressBar.click(function (e) {
                // 获取滚动条距离窗口的默认位置
                var normalLeft = $(this).offset().left;
                // 获取点击的位置距离窗口的位置
                var eLeft = e.pageX;
                // 设置前景的宽度
                $this.$progressLoad.css('width', eLeft - normalLeft);
                $this.$progressPlay.css('left', eLeft - normalLeft - 4);
                // 计算进度条比例
                var value = (eLeft - normalLeft) / $(this).width();
                callBack(value);
            });
        },
        progressMove: function (callBack) {
            var $this = this;
            // 获取滚动条距离窗口的默认位置
            var normalLeft = this.$progressBar.offset().left;
            var eLeft;
            // 监听鼠标按下事件
            this.$progressBar.mousedown(function () {
                $this.isMove = true;
                // 监听鼠标移动事件
                $(document).mousemove(function (e) {
                    // 获取点击的位置距离窗口的位置
                    eLeft = e.pageX;
                    var x = eLeft - normalLeft;
                    if (x >= 0 && x <= $this.$progressBar.width()) {
                        // 设置前景的宽度
                        $this.$progressLoad.css('width', x);
                        $this.$progressPlay.css('left', x - 4);
                    }
                })
            });
            // 监听鼠标抬起事件
            $(document).mouseup(function () {
                $(document).off('mousemove');
                $this.isMove = false;
                // 计算进度条比例
                var value = (eLeft - normalLeft) / $this.$progressBar.width();
                callBack(value);
            })
        },
        setProgress: function (value) {
            if (this.isMove) return;
            if (value < 0 || value > 100) return;
            this.$progressLoad.css({
                width: value + '%',
            });
            this.$progressPlay.css({
                left: value + '%',
            });
        }
    }
    Progress.prototype.init.prototype = Progress.prototype;
    window.Progress = Progress;
})(window)